package com.snake.springboot.authority.service.impl;

import com.snake.springboot.authority.api.model.AuthMenuModel;
import com.snake.springboot.authority.api.model.AuthRoleMenuModel;
import com.snake.springboot.authority.api.response.AuthMenuComplexRsp;
import com.snake.springboot.authority.api.response.AuthMenuRsp;
import com.snake.springboot.authority.api.service.AuthMenuService;
import com.snake.springboot.authority.dao.mybatis.entity.*;
import com.snake.springboot.authority.dao.mybatis.mappers.AuthMenuInterfaceMapper;
import com.snake.springboot.authority.dao.mybatis.mappers.AuthMenuMapper;
import com.snake.springboot.authority.service.assembler.AuthMenuAssembler;
import com.snake.springboot.authority.service.assembler.AuthMenuInterfaceAssembler;
import com.snake.springboot.authority.service.assembler.AuthRoleMenuAssembler;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.snake.common.utils.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 类 {@code AuthMenuServiceImpl}  <br> .
 * <p>权限菜单服务实现类</p>
 * 创建时间：2024/5/27 14:10
 *
 * @author Yzhui
 * @see
 */
@Service
public class AuthMenuServiceImpl implements AuthMenuService {

    private AuthMenuMapper authMenuMapper;

    private AuthMenuInterfaceMapper authMenuInterfaceMapper;

    @Autowired
    public AuthMenuServiceImpl(AuthMenuMapper authMenuMapper, AuthMenuInterfaceMapper authMenuInterfaceMapper) {
        this.authMenuMapper = authMenuMapper;
        this.authMenuInterfaceMapper = authMenuInterfaceMapper;
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public AuthMenuModel addMenu(AuthMenuModel model) throws Exception {
        AuthMenuDO entity = AuthMenuAssembler.assembleAuthMenuDO(model, true);
        List<AuthMenuInterfaceDO> interfaces = AuthMenuInterfaceAssembler
                                        .assembleAuthMenuInterfaceDOList(model.getInterfaceIds(), entity.getMenuId());
        authMenuMapper.insert(entity);
        if(CollectionUtils.isNotEmpty(interfaces)) {
            authMenuInterfaceMapper.insertBatch(interfaces);
        }
        model.setMenuId(entity.getMenuId());
        return model;
    }

    @Override
    public int removeMenu(String menuId) {
        authMenuInterfaceMapper.delete(new AuthMenuInterfaceDO(menuId, null));
        return authMenuMapper.delete(menuId);
    }

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public int modifyMenu(AuthMenuModel model) throws Exception {
        authMenuInterfaceMapper.delete(new AuthMenuInterfaceDO(model.getMenuId(), null));
        if(CollectionUtils.isNotEmpty(model.getInterfaceIds())) {
            List<AuthMenuInterfaceDO> interfaces = AuthMenuInterfaceAssembler
                    .assembleAuthMenuInterfaceDOList(model.getInterfaceIds(), model.getMenuId());
            authMenuInterfaceMapper.insertBatch(interfaces);
        }
        AuthMenuDO entity = AuthMenuAssembler.assembleAuthMenuDO(model, false);
        return authMenuMapper.update(entity);
    }


    @Override
    public void modifyMenuEnable(AuthMenuModel model) {
        AuthMenuDO entity =new AuthMenuDO();
        entity.setEnable(model.getEnable());
        entity.setMenuId(model.getMenuId());
        authMenuMapper.update(entity);
    }

    @Override
    public void modifyMenuSort(AuthMenuModel model) {
        AuthMenuDO entity =new AuthMenuDO();
        entity.setSortIndex(model.getSortIndex());
        entity.setMenuId(model.getMenuId());
        authMenuMapper.update(entity);
    }

    @Override
    public AuthMenuComplexRsp querySingleMenu(String menuId) {
        List<AuthMenuComplexDO> list = authMenuMapper.findById(menuId);
        AuthMenuComplexDO entity = null;
        AuthMenuComplexRsp result = null;
        if(CollectionUtils.isNotEmpty(list)) {
            entity = list.get(0);
            List<AuthMenuInterfaceDO> interfaceList = entity.getMenuInterfaceList();
            if(CollectionUtils.isNotEmpty(interfaceList)) {
                if(StringUtils.isEmpty(interfaceList.get(0).getInterfaceName())
                    && StringUtils.isEmpty(interfaceList.get(0).getInterfaceId()))  {
                    entity.setMenuInterfaceList(new ArrayList<>(1));
                }
            }
            result = AuthMenuAssembler.assembleAuthMenuComplexRsp(entity);
        }
        return result;
    }

    @Override
    public List<AuthMenuRsp> queryMenuTree(AuthRoleMenuModel params){
        AuthRoleMenuDO condition = AuthRoleMenuAssembler.assembleAuthRoleMenuDO(params);
        List<AuthMenuTreeDO> list = authMenuMapper.findForMenuTree(condition);
        List<AuthMenuRsp> result = ListUtils.copy(list, AuthMenuRsp.class);
        return result;
    }
}
